package com.saturday.auth.impl;

import cn.hutool.core.bean.copier.CopyOptions;
import com.saturday.common.exception.UserContextLoseException;
import com.saturday.common.model.UserContext;
import com.saturday.common.support.ISessionCloudApi;
import com.saturday.common.util.BeanUtils;
import org.apache.commons.collections4.MapUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SessionCloudApiImpl implements ISessionCloudApi {

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public UserContext getUserContextBySessionId(String sessionId) {
        Map<Object, Object> userMap = redissonClient.getMap(sessionId).readAllMap();
        if (MapUtils.isEmpty(userMap)) {
            throw UserContextLoseException.createException();
        }
        return BeanUtils.mapToBean(userMap, UserContext.class, true, CopyOptions.create());
    }
}
